console.log("load success.");
let imgIndex = 0;

const socketClient = io("http://127.0.0.1:3000");
//检测到"connect"事件（客户端连接到服务器）时触发第二个参数所指向的函数
socketClient.on("connect", ()=>{
    console.log("client connect success.");
});

socketClient.on("disconnect", ()=>{
    console.log("client disconnect.");
})


function Click(event){
    console.log(event);
    axios.get("http://127.0.0.1:3000/user2").then((res)=>{
        console.log(res.data);
        document.getElementById("name").innerText = res.data.name;
        document.getElementById("age").innerText = res.data.age;
        document.getElementById("imga").src = "data:image/bmp;base64," + res.data.avatar;
    }).catch((err)=>{
        console.error(err);
    })
}

function changeImg(event){
    if(imgIndex==0){
        document.getElementById("img1").style.display = "none";
        document.getElementById("img2").style.display = "block";
        imgIndex = 1;
    }
    else if(imgIndex==1){
        document.getElementById("img1").style.display = "block";
        document.getElementById("img2").style.display = "none";
        imgIndex = 0;
    }
}

function updateImg(data){
    document.getElementById("name").innerText = data.data.name;
    document.getElementById("age").innerText = data.data.age;
    document.getElementById("imga").src = "data:image/jpg;base64," + data.data.avatar;
}

function getUserInfoBySocket(event){
    socketClient.emit("user", "123455", (data)=>{
        console.log("socket", data);
        console.log(data.message);
        if(data.result==true){
            updateImg(data);
        }
        else if(data.result==false){
            alert("请求信息出错");
        }
    })
}

socketClient.on("count", (data)=>{
    console.log(data);
    document.getElementById("age").innerText = data;
})